<?php

namespace app\api\controller\meizhuang;

use app\api\controller\Controller;
use app\api\model\meizhuang\Ask as AskModel;
use think\Request;
use think\Db;
use app\api\model\Goods as GoodsModel;
/**
 * 美妆-收藏控制器
 */
class Collect extends Controller{

    protected $askModel;
    protected $tag=[];

    public function _initialize(Request $request = null){
        $this->askModel=new AskModel();
        $this->tag=[
            '飞粉','正品','色号','质量','黑黄皮','适合淡妆','容易上妆','易过敏'
        ];
    }
    /*
     * 收藏
     */
    public function add(){
        $goods_id=input('post.goods_id');
        $user = $this->getUser(false);
        $user_id=$user['user_id'];
        if(empty($goods_id)){
            return $this->renderError([],'商品id不能为空');
        }
        if(empty($user_id)){
            return $this->renderError([],'用户id不能为空');
        }
        //存储商品收藏时的价格
        $res=Db::name('goods_sku')->where(['goods_id'=>$goods_id])->field('goods_price')->find();
        if(empty($res)){
            return $this->renderError([],'未获取到商品sku信息');
        }
        $data=[
            'goods_id'=>$goods_id,
            'money'=>$res['goods_price'],
            'user_id'=>$user_id,
            'create_time'=>time(),
            'wxapp_id'=>input('post.wxapp_id')
        ];
        Db::name('meizhuang_collect')->insertGetId($data);
        return $this->renderSuccess([],'收藏成功');
    }
    /*
     * 取消收藏
     */
    public function del(){
        $goods_id=input('post.goods_id');
        $user = $this->getUser(false);
        $user_id=$user['user_id'];
        if(empty($goods_id)){
            return $this->renderError([],'商品id不能为空');
        }
        if(empty($user_id)){
            return $this->renderError([],'用户id不能为空');
        }
        Db::name('meizhuang_collect')->where(['goods_id'=>$goods_id,'user_id'=>$user_id])->delete();
        return $this->renderSuccess([],'取消成功');
    }
    /*
     * 从收藏列表取消收藏
     */
    public function cancel(){
        $id=input('post.id');
        if(empty($id)){
            return $this->renderError([],'收藏id不能为空');
        }
        Db::name('meizhuang_collect')->where('id','in',$id)->delete();
        return $this->renderSuccess([],'取消成功');
    }
    /*
     * 收藏列表
     */
    public function list(){
        //需要添加tab栏筛选
        $type=input('post.type');//0：全部 1:有优惠  2：已买过
        $user = $this->getUser(false);
        $user_id=$user['user_id'];
        if(!isset($type)){
            return $this->renderError([],'type为必填');
        }
        if(empty($user_id)){
            return $this->renderError([],'用户id不能为空');
        }
        $str1='';
        $str2='';
        if($type==1){
            //获取当前商品的价格
            //获取当前用户的所有收藏
            $collectInfo=Db::name('meizhuang_collect')->where(['user_id'=>$user_id,'wxapp_id'=>input('post.wxapp_id')])->select();
            if($collectInfo){
                //将符合条件的good_id放入数组
                $ids=[];
                foreach ($collectInfo as $k=>$v){
                    $goodsInfo=Db::name('goods_sku')->where(['goods_id'=>$v['goods_id']])->field('goods_price')->find();
                    if($v['money']>$goodsInfo['goods_price']){
                        $ids[]=$v['goods_id'];
                    }
                }
            }
            $str1='c.goods_id';
            $str2='in';
            $str3=$ids;
        }elseif ($type==2){
            //获取当前用户购买过的商品id
            $str1='c.goods_id';
            $str2='in';
            $goodsOrderInfo=Db::name('order_goods')->where(['user_id'=>$user_id])->field('goods_id')->select();
            $str=[];
            if($goodsOrderInfo){
                foreach ($goodsOrderInfo as $k=>$v){
                    $str[]=$v['goods_id'];
                }
            }
            $str3=$str;
        }else{
            $str1='c.user_id';
            $str2='=';
            $str3=$user_id;
        }
        $list=Db::name('meizhuang_collect')
            ->alias('c')
            ->join('yoshop_goods g','c.goods_id = g.goods_id')
            ->where('c.user_id','=',$user_id)
            ->where($str1, $str2 ,$str3)
            ->order('c.create_time desc')
            ->select();
        $list=$list->toArray();
        $model = new GoodsModel;
        if($list){
            foreach($list as $k=>$v){
                $goodsInfo = $model->getDetails($v['goods_id'], $this->getUser(false));
                $list[$k]['goodsInfo']=$goodsInfo;
                //获取图片地址,只获取一张图片的地址
                $imageId=Db::name('goods_image')->where(['goods_id'=>$v['goods_id']])->field('image_id')->find();
                $uploadInfo=Db::name('upload_file')->where(['file_id'=>$imageId['image_id']])->find();
                $list[$k]['imageUrl']=$uploadInfo['file_url'].'/'.$uploadInfo['file_name'];
            }
        }
        return $this->renderSuccess($list,'获取成功');
    }
}
